package com.oasystem.web.view;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;

import com.oasystem.domain.ProfitDetailGbptBean;
import com.oasystem.domain.ProfitReturnedRecord;
import com.oasystem.utils.DateUtils;

public class ProfitTableGbptView extends AbstractView {

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ProfitDetailGbptBean profitDetailGbptBean = (ProfitDetailGbptBean) model
				.get("profitDetails");
		String fileName = "profitReturned.gbpt";// 设置下载时客户端Excel的名称
		response.setContentType("application/octet-stream");
		response.setHeader("Content-disposition", "attachment;filename="
				+ fileName);
		OutputStream ouputStream = response.getOutputStream();

		String currentDate = DateUtils.getTodayDate("yyyyMMdd");

		BufferedReader br = new BufferedReader(new InputStreamReader(this
				.getClass().getResourceAsStream("/gbptTemplate.properties"),
				"UTF-8"));
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				ouputStream));
		String data = null;
		boolean summaryBegin = false, detailBegin = false;
		while ((data = br.readLine()) != null) {
			if ("?总计信息行开始".equals(data)) {
				summaryBegin = true;
				continue;
			} else if ("?明细行行开始".equals(data)) {
				detailBegin = true;
				continue;
			}

			if (summaryBegin) {
				// RMB|?DATE|1|?SUM|?COUNT|
				StringBuilder sb = new StringBuilder(data);
				int index = sb.indexOf("?DATE");
				sb.replace(index, index + "?DATE".length(), currentDate);
				index = sb.indexOf("?SUM");
				sb.replace(index, index + "?SUM".length(), profitDetailGbptBean
						.getSum().toString());
				index = sb.indexOf("?COUNT");
				sb.replace(index, index + "?COUNT".length(), String
						.valueOf(profitDetailGbptBean
								.getProfitReturnedRecordList().size()));
				summaryBegin = false;
				writer.write(sb.toString());
				writer.newLine();
			} else if (detailBegin) {
				// RMB|?日期|?顺序号|6222081209000668296|灵通卡|?收款帐号|?收款帐号名称|?金额|||1|?手机|?身份证号码|
				int strIndex = 0, iterateIndex = 1;
				for (ProfitReturnedRecord detail : profitDetailGbptBean
						.getProfitReturnedRecordList()) {
					StringBuilder tmp = new StringBuilder(data);
					strIndex = tmp.indexOf("?日期");
					tmp.replace(strIndex, strIndex + "?日期".length(),
							currentDate);
					strIndex = tmp.indexOf("?顺序号");
					tmp.replace(strIndex, strIndex + "?顺序号".length(),
							String.valueOf(iterateIndex++));
					strIndex = tmp.indexOf("?收款帐号");
					tmp.replace(strIndex, strIndex + "?收款帐号".length(),
							detail.getMemberCardNumber());
					strIndex = tmp.indexOf("?收款帐号名称");
					tmp.replace(strIndex, strIndex + "?收款帐号名称".length(),
							detail.getName());
					strIndex = tmp.indexOf("?金额");
					tmp.replace(strIndex, strIndex + "?金额".length(),
							detail.getTodayReturnAmount());
					strIndex = tmp.indexOf("?手机");
					tmp.replace(strIndex, strIndex + "?手机".length(),
							detail.getPhoneNumber());
					strIndex = tmp.indexOf("?身份证号码");
					tmp.replace(strIndex, strIndex + "?身份证号码".length(),
							detail.getIdCardNumber());
					writer.write(tmp.toString());
					writer.newLine();
				}
				detailBegin = false;
			} else {
				writer.write(data);
				writer.newLine();
			}
		}
		writer.flush();
		writer.close();
		br.close();
	}
}
